#include "link_stack.h"

int main(int argc, const char *argv[]){
    stack_t *my_stack = NULL;
    // 创建栈
    create_stack(&my_stack);

    char buff[1024] = {0};
    gets(buff);
    printf("%s\n", buff);

    int i = 0;
    int value = 0; // 保存出栈的元素
    while(buff[i]){
        switch(buff[i]){
            case '(':
            case '[':
            case '{':
                push_stack(my_stack, buff[i]);
                break;
            case ')':
                if(pop_stack(my_stack, &value) || value != '('){
                    goto NEXT;
                }
                break;
            case ']':
                if(pop_stack(my_stack, &value) || value != '['){
                    goto NEXT;
                }
                break;
            case '}':
                if(pop_stack(my_stack, &value) || value != '{'){
                    goto NEXT;
                }
                break;
        }
        i++;
    }
NEXT:
    if(buff[i] == '\0'){
        // 如果执行到这里 说明上面的循环是自然结束的
        if(is_empty(my_stack)){
            printf("字符串中括号配对\n");
        }else{
            printf("字符串中括号不配对\n");
        }
    }else{
        // 如果执行到这里 说明 上面的while循环是goto到这里的
        int j = 0;
        for(j = 0; j < i; j++){
            printf(" ");
        }
        printf("^\n");
        printf("字符串中括号不配对\n");
    }

    // 销毁栈
    destroy_stack(&my_stack);

    return 0;
}